package com.gonsin.base;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 搜索结果（翻页控件）
 * @param <T>
 */
@Data
@ApiModel("搜索结果（翻页控件）")
public class GPageResult<T> {

    @ApiModelProperty("搜索实体类结果")
    private List<T> data;

    /**
     * 搜索当前页码
     */
    @ApiModelProperty("搜索当前页码")
    private int page;

    /**
     * 每页个数
     */
    @ApiModelProperty("每页个数")
    private int count;

    /**
     * 搜索总数
     */
    @ApiModelProperty("搜索总数")
    private int totalCount;

    /**
     * 页码总数
     */
    @ApiModelProperty("页码总数")
    private int pageCount;

    /**
     * 生成返回结果
     */
    public static <D> GPageResult<D> result(List<D> data, int totalCount, GPageRequest pageRequest){
        GPageResult<D> result = new GPageResult<>();
        result.setData(data);
        result.setTotalCount(totalCount);
        result.setPage(pageRequest.getPage());
        result.setCount(pageRequest.getCount());
        result.setPageCount(totalCount / pageRequest.getCount() + (totalCount % pageRequest.getCount() == 0 ? 0 : 1));
        return result;
    }


}

